/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 07/10/2011, 10:22:54
 */
package Run;

import Negocio.ArbolBusqueda;
import Negocio.Hoja;
import Presetacion.ArbolCellRender;
import Presetacion.Tablero;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author Ivan
 */
public class VentanaDetalle extends javax.swing.JDialog {

    /** Creates new form Principal */
    private ArbolBusqueda arbol;
    private DefaultMutableTreeNode top;
    private JTree jArbol;
    private javax.swing.JScrollPane jScrollPane1;
    private ArrayList<Tablero> tableros;

    public VentanaDetalle(java.awt.Frame parent, boolean modal, ArbolBusqueda arbol, Tablero tabModelo) {
        super(parent, modal);
        jScrollPane1 = new javax.swing.JScrollPane();
        initComponents();
        this.arbol = arbol;

        arbolIniciar();

        tableros = new ArrayList<Tablero>(5);
        tableros.add(tbPrincipal);
        tableros.add(tbHoja1);
        tableros.add(tbHoja2);
        tableros.add(tbHoja3);
        tableros.add(tbHoja4);

        for (Tablero tab : tableros) {
            tab.iniciar(tabModelo.getColumnas(), tabModelo.getFilas(), tabModelo.getImagenURL());            
            tab.setEnabled(false);
            tab.setEstado(null);
        }
       jArbol.setSelectionRow(0);
       jArbol.setCellRenderer(new ArbolCellRender());
  //     this.pack();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void arbolIniciar() {

        top = new DefaultMutableTreeNode(arbol.getRaiz());
        createNodes(top);

        //Create a tree that allows one selection at a time.
        jArbol = new JTree(top);
        jArbol.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);


        jArbol = new javax.swing.JTree(top);

        jArbol.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {

            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jArbolValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jArbol);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE).addContainerGap()));

        this.pack();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        btnSalir = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        tbPrincipal = new Presetacion.Tablero();
        pnlContenedor = new javax.swing.JPanel();
        tbHoja1 = new Presetacion.Tablero();
        tbHoja2 = new Presetacion.Tablero();
        tbHoja3 = new Presetacion.Tablero();
        tbHoja4 = new Presetacion.Tablero();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 335, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 468, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.LINE_START);

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(726, Short.MAX_VALUE)
                .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnSalir)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel3, java.awt.BorderLayout.PAGE_END);

        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        tbPrincipal.setBorder(javax.swing.BorderFactory.createTitledBorder("Nodo seleccionado"));

        javax.swing.GroupLayout tbPrincipalLayout = new javax.swing.GroupLayout(tbPrincipal);
        tbPrincipal.setLayout(tbPrincipalLayout);
        tbPrincipalLayout.setHorizontalGroup(
            tbPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 446, Short.MAX_VALUE)
        );
        tbPrincipalLayout.setVerticalGroup(
            tbPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 109, Short.MAX_VALUE)
        );

        pnlContenedor.setBorder(javax.swing.BorderFactory.createTitledBorder("Hijos"));
        pnlContenedor.setLayout(new java.awt.GridLayout(2, 2));

        tbHoja1.setBorder(javax.swing.BorderFactory.createTitledBorder("Hijo [1]"));

        javax.swing.GroupLayout tbHoja1Layout = new javax.swing.GroupLayout(tbHoja1);
        tbHoja1.setLayout(tbHoja1Layout);
        tbHoja1Layout.setHorizontalGroup(
            tbHoja1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );
        tbHoja1Layout.setVerticalGroup(
            tbHoja1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 99, Short.MAX_VALUE)
        );

        pnlContenedor.add(tbHoja1);

        tbHoja2.setBorder(javax.swing.BorderFactory.createTitledBorder("Hijo [2]"));

        javax.swing.GroupLayout tbHoja2Layout = new javax.swing.GroupLayout(tbHoja2);
        tbHoja2.setLayout(tbHoja2Layout);
        tbHoja2Layout.setHorizontalGroup(
            tbHoja2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );
        tbHoja2Layout.setVerticalGroup(
            tbHoja2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 99, Short.MAX_VALUE)
        );

        pnlContenedor.add(tbHoja2);

        tbHoja3.setBorder(javax.swing.BorderFactory.createTitledBorder("Hijo [3]"));

        javax.swing.GroupLayout tbHoja3Layout = new javax.swing.GroupLayout(tbHoja3);
        tbHoja3.setLayout(tbHoja3Layout);
        tbHoja3Layout.setHorizontalGroup(
            tbHoja3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );
        tbHoja3Layout.setVerticalGroup(
            tbHoja3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 99, Short.MAX_VALUE)
        );

        pnlContenedor.add(tbHoja3);

        tbHoja4.setBorder(javax.swing.BorderFactory.createTitledBorder("Hijo [4]"));

        javax.swing.GroupLayout tbHoja4Layout = new javax.swing.GroupLayout(tbHoja4);
        tbHoja4.setLayout(tbHoja4Layout);
        tbHoja4Layout.setHorizontalGroup(
            tbHoja4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );
        tbHoja4Layout.setVerticalGroup(
            tbHoja4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 99, Short.MAX_VALUE)
        );

        pnlContenedor.add(tbHoja4);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(pnlContenedor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE)
                    .addComponent(tbPrincipal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tbPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlContenedor, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                .addContainerGap())
        );

        getContentPane().add(jPanel4, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed
    private void seleccionTree() {
        // TODO add your handling code here:
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) jArbol.getLastSelectedPathComponent();
        if (node == null) {
            return;
        }
        Object nodeInfo = node.getUserObject();
        Hoja book = (Hoja) nodeInfo;
        // txtMostrar.setText(book.getEstado().toString());

        for (Tablero tab : tableros) {
            tab.setEstado(null);
        }

        tableros.get(0).setEstado(book.getEstado());

        List<Hoja> hojas = book.getHijos();
        for (int i = 0; i < hojas.size(); i++) {
            tableros.get(i+1).setEstado(hojas.get(i).getEstado());
        }


        super.pack();



    }

    /**
     * @param args the command line arguments
     */
    private void jArbolValueChanged(javax.swing.event.TreeSelectionEvent evt) {
        // TODO add your handling code here:
        seleccionTree();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalir;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel pnlContenedor;
    private Presetacion.Tablero tbHoja1;
    private Presetacion.Tablero tbHoja2;
    private Presetacion.Tablero tbHoja3;
    private Presetacion.Tablero tbHoja4;
    private Presetacion.Tablero tbPrincipal;
    // End of variables declaration//GEN-END:variables

    private void createNodes(DefaultMutableTreeNode top) {
        DefaultMutableTreeNode category = null;
        Hoja raiz = (Hoja) top.getUserObject();
        if (raiz.getHijos().isEmpty()) {
            return;
        }
        for (int i = 0; i < raiz.getHijos().size(); i++) {
            category = new DefaultMutableTreeNode(raiz.getHijos().get(i));
            top.add(category);
            createNodes(category);
        }
    }
}
